package com.flashcash.solu.view;

import android.text.TextUtils;
import android.util.Pair;

import com.flashcash.solu.app.base.presenter.BaseActivityPresenterImpl;
import com.flashcash.solu.bean.LatestLoanAppBean;
import com.flashcash.solu.bean.ProgressBean;
import com.flashcash.solu.common.FieldParams;
import com.flashcash.solu.common.FieldParamsInt;
import com.flashcash.solu.common.TokenManager;
import com.x.leo.apphelper.data.cache.CacheDataManage;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @作者:My
 * @创建日期: 2017/6/21 17:59
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */

public class IntroduceActPreImp extends BaseActivityPresenterImpl implements IntroduceActPresenter {

    @Override
    public void display() {

    }

    @Override
    public void updateCertifyState(boolean showProcess) {
        if (showProcess) {
            showLoading("");
        }
        mUserApi.progress(TokenManager.getInstance().getToken())
                .zipWith(mUserApi.getLatestLoanApp(TokenManager.getInstance().getToken(), "certifyFragment"), Pair::create)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Pair<ProgressBean, LatestLoanAppBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (showProcess) {
                            dismissLoading();
                        }
                    }

                    @Override
                    public void onNext(Pair<ProgressBean, LatestLoanAppBean> progressBeanLatestLoanAppBeanPair) {
                        if (showProcess) {
                            dismissLoading();
                        }
                        if (progressBeanLatestLoanAppBeanPair.first != null) {
                            ((IntroduceActView) mView).updateCertifyStateSuccess(progressBeanLatestLoanAppBeanPair.first);
                            LatestLoanAppBean loanAppBean = progressBeanLatestLoanAppBeanPair.second;
                            boolean isEditable = false;
                            if (loanAppBean == null) {
                                isEditable = true;
                            } else {
                                String status = loanAppBean.getStatus();
                                if (TextUtils.equals(status, FieldParams.LoanStatus.CLOSED)
                                        || TextUtils.equals(status, FieldParams.LoanStatus.REJECTED)
                                        || TextUtils.equals(status, FieldParams.LoanStatus.PAID_OFF)
                                        || TextUtils.equals(status, FieldParams.LoanStatus.WITHDRAWN)
                                        || TextUtils.equals(status, FieldParams.LoanStatus.NOLOANAPP)) {
                                    isEditable = true;
                                }
                            }
                            CacheDataManage.INSTANCE.addData(FieldParamsInt.INFO_EDITABLE, isEditable);
                        }
                    }
                });
    }

}
